<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>地区管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
            </div>

            <table id="table" lay-filter="table"></table>
            <script type="text/html" id="operation">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
            </script>
        </div>
    </div>
</div>
<!--<script type="text/html" id="typeTpl">-->
<!--<div class="layui-input-block" style="margin:0 auto">-->
<!--<input value="{{d.rol_id}}" type="checkbox" {{#  if(d.rol_type ==1){ }} checked {{#  } else { }} {{#  } }} name="open" lay-skin="switch" lay-filter="switchTest" lay-text="启用|禁用" >-->
<!--</div>-->
<!--</script>-->
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        //创建表格
        table.render({
            elem: '#table'
            ,url:"{:url('admin/Information/locationList')}"
            ,cols: [[
                {field: 'attr_id', width: 80, title: 'ID', sort: true,align: 'center'}
                ,{field: 'attr_name', title: '地区名称',align: 'center'}
                ,{field: 'attr_order', title: '地区排序',align: 'center'}
                ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#operation'}
            ]]
            , text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });

        //监听工具条
        table.on('tool(table)', function(obj){
            var data = obj.data;
             if(obj.event === 'edit'){
                var tr = $(obj.tr);
                layer.open({
                    type: 2
                    ,title: '编辑地区'
                    ,content: "{:url ('admin/Information/locationEdit')}?primary_id="+obj.data.attr_id
                    ,area: ['600px', '250px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(submit)', function(data){
                            var field = data.field; //获取提交的字段

                            $.ajax({
                                url: "{:url ('admin/Information/locationEdit')}?primary_id="+obj.data.attr_id,
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.close(index); //关闭弹层
                                        layer.msg('编辑成功', {icon: 1, time: 1500}, function () {
                                            table.reload('table'); //数据刷新
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                        });
                        submit.trigger('click');
                    }
                    ,success: function(layero, index){
                    }
                })
            }
        });



        //事件
        var active = {
            //添加地区
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加新地区'
                    ,content:"{:url ('admin/Information/locationAdd')}"
                    ,area: ['600px', '250px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(submit)', function(data){
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url: "{:url ('admin/Information/locationAdd')}",
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.close(index); //关闭弹层
                                        layer.msg('添加成功！', {icon: 1, time: 1500}, function () {
                                            //重载表格
                                            table.reload('table');
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                        });
                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-role').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

